package com.cskaoyan.service.user.impl;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketSearchHistory;
import com.cskaoyan.bean.po.MarketSearchHistoryExample;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.service.user.UserHistoryService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jzp
 * @since 2022/11/19 22:09
 */
@Service
public class UserHistoryServiceImpl implements UserHistoryService {
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public CommonData list(BasePageInfo info, String keyword, String userId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 开启sql
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(userId)) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            }catch (Exception e){
                return null;
            }
        }
        if (!StringUtil.isEmpty(keyword)) {
            String trim = keyword.trim();
            criteria.andKeywordLike("%" + trim + "%");
        }
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example);

        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(pageInfo);
    }
}
